package top.ruanlitao.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.ruanlitao.shortlink.project.dao.entity.LinkAccessLogsDO;
import top.ruanlitao.shortlink.project.dao.entity.LinkAccessStatsDO;
import top.ruanlitao.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 访问日志监控持久层
 */
public interface LinkAccessLogsMapper extends BaseMapper<LinkAccessLogsDO> {

    /**
     * 根据短链接获取指定日期内高频访问IP数据
     */
    @Select("SELECT " +
            "    tlal.ip, " +
            "    COUNT(tlal.ip) AS count " +
            "FROM " +
            "    t_link tl INNER JOIN " +
            "    t_link_access_logs tlal ON tl.full_short_url = tlal.full_short_url " +
            "WHERE " +
            "    tlal.full_short_url = #{param.fullShortUrl} " +
            "    AND tl.gid = #{param.gid} " +
            "    AND tl.del_flag = '0' " +
            "    AND tl.enable_status = #{param.enableStatus} " +
            "    AND tlal.create_time BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    tlal.full_short_url, tl.gid, tlal.ip " +
            "ORDER BY " +
            "    count DESC " +
            "LIMIT 5;")
    List<HashMap<String, Object>> listTopIpByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据短链接获取指定日期内PV、UV、UIP数据
     */
    @Select("""
        SELECT
            COUNT(tlal.user) AS pv,
            COUNT(DISTINCT tlal.user) AS uv,
            COUNT(DISTINCT tlal.ip) AS uip
        FROM
            t_link tl INNER JOIN
            t_link_access_logs tlal ON tl.full_short_url = tlal.full_short_url
        WHERE
            tlal.full_short_url = #{param.fullShortUrl}
            AND tl.gid = #{param.gid}
            AND tl.del_flag = '0'
            AND tl.enable_status = #{param.enableStatus}
            AND tlal.create_time BETWEEN #{param.startDate} and #{param.endDate}
        GROUP BY
            tlal.full_short_url, tl.gid
    """)
    LinkAccessStatsDO findPvUvUidStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据短链接获取指定日期内新旧访客数据
     */
    /*@Select("""
        SELECT
            SUM(old_user) AS oldUserCnt,
            SUM(new_user) AS newUserCnt
        FROM (
            SELECT
                CASE WHEN COUNT(DISTINCT DATE(tlal.create_time)) > 1 THEN 1 ELSE 0 END AS old_user,
                CASE WHEN COUNT(DISTINCT DATE(tlal.create_time)) = 1 AND MAX(tlal.create_time) >= #{param.startDate} AND MAX(tlal.create_time) <= #{param.endDate} THEN 1 ELSE 0 END AS new_user
            FROM
                t_link tl INNER JOIN
                t_link_access_logs tlal ON tl.full_short_url = tlal.full_short_url
            WHERE
                tlal.full_short_url = #{param.fullShortUrl}
                AND tl.gid = #{param.gid}
                AND tl.enable_status = #{param.enableStatus}
                AND tl.del_flag = '0'
            GROUP BY
                tlal.user
        ) AS user_counts
    """)*/
    @Select("""
        SELECT
            SUM(CASE WHEN first_access_time BETWEEN #{param.startDate} AND #{param.endDate} THEN 1 ELSE 0 END) AS newUserCnt,
            SUM(CASE WHEN access_count_in_period > 1 THEN 1 ELSE 0 END) AS oldUserCnt
        FROM (
            SELECT
                tlal.user,
                MIN(tlal.create_time) AS first_access_time,
                COUNT(CASE WHEN tlal.create_time BETWEEN #{param.startDate} AND #{param.endDate} THEN 1 END) AS access_count_in_period
            FROM
                t_link tl
            INNER JOIN
                t_link_access_logs tlal ON tl.full_short_url = tlal.full_short_url
            WHERE
                tl.full_short_url = #{param.fullShortUrl}
                AND tl.gid = #{param.gid}
                AND tl.enable_status = #{param.enableStatus}
                AND tl.del_flag = '0'
            GROUP BY
                tlal.user
        ) AS user_access_summary;
    """)
    HashMap<String, Object> findUvTypeCntByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);


    /**
     * 获取用户信息是否新老访客
     */
    @MapKey("user")
    List<Map<String, Object>> selectUvTypeByUsers(
            @Param("gid") String gid,
            @Param("fullShortUrl") String fullShortUrl,
            @Param("enableStatus") Integer enableStatus,
            @Param("startDate") String startDate,
            @Param("endDate") String endDate,
            @Param("userAccessLogsList") List<String> userAccessLogsList
    );
}